<?php

namespace app\controllers;

use Yii;
use app\models\User;
use app\models\UserSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * UserController implements the CRUD actions for User model.
 */
class UserController extends CController
{
	public $layout='main';
	
	public function actionLogin()
    {
        $this -> layout='loginLayout';

        Yii::info("VH: Login");

    	if(Yii::$app -> request->isPost){
    		Yii::$app->response->format = 'json';

            $username = $_POST["username"];
            $password = $_POST['password'];

            $modelUser = User::findOne(["username" => $username]);

    		if( $modelUser != null && $modelUser -> Password == $password)
    		{
                Yii::$app -> session -> set("user", $modelUser);

    			return ["status" => "success"];
    		}
    		else 
    			return ["status" => "failure"];
    	}
    	else 
    	{
    		return $this->render('login');
    	}
    }

    public function actionLogout()
    {
        Yii::info("VH: Logout");

        Yii::$app -> session -> removeAll();

        $this -> redirect("/user/login");
    }

    public function actionChangepassword()
    {
        $this -> layout='loginLayout';

        return $this->render('changePassword');
    }

    public function actionCheckpassword()
    {
        if(Yii::$app -> request -> isPost){
            Yii::$app->response->format = 'json';
            $user = Yii::$app -> session -> get("user");

            $userModel = User::findIdentity($user -> UserID);

            if($_POST['password'] == $userModel -> Password)
            {
                return ["status" => "success"];
            }
            else
            {
                return ["status" => "failure"];
            }
        }

        return ["status" => "failure"];
    }

    public function actionUpdatepassword()
    {
        Yii::$app->response->format = 'json';

        $user = Yii::$app -> session -> get("user");
        $userModel = User::findIdentity($user -> UserID);

        $userModel -> Password = $_POST['new_password'];

        if($userModel -> save())
        {
            return ["status" => "success"];
        }
        else
        {
            return ["status" => "failure"];
        }
    }

    public function actionList()
    {
        if(Yii::$app -> request->isPost){
            \Yii::$app->response->format = 'json';

            $query = new \yii\db\Query();
            $query -> select('*') -> from('user');
            $query -> where('DeleteFlag=0');
            $list = $query -> all();

            return $list;
        }
        else
        {
            return $this->render('list');
        }
    }

    /**
     * Creates a new User model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new User();

        if(Yii::$app->request->isPost){
            $model -> load(Yii::$app->request->post());
            $model -> save();

            $model->refresh();
            Yii::$app->response->format = 'json';

            return [
                'message' => 'Success!!!',
            ];
        }
        else {
            return $this->renderAjax('create', [
                'model' => $model,
            ]);
        }
    }

}
